<h1 id="Live2D-Widget"><a href="#Live2D-Widget" class="headerlink" title="Live2D Widget"></a>Live2D Widget</h1><p><img src="https://forthebadge.com/images/badges/built-with-love.svg"><br><img src="https://forthebadge.com/images/badges/uses-html.svg"><br><img src="https://forthebadge.com/images/badges/made-with-javascript.svg"><br><img src="https://forthebadge.com/images/badges/contains-cat-gifs.svg"><br><img src="https://forthebadge.com/images/badges/powered-by-electricity.svg"><br><img src="https://forthebadge.com/images/badges/makes-people-smile.svg"></p>
<p><a href="README.md">中文</a></p>
<h2 id="Features"><a href="#Features" class="headerlink" title="Features"></a>Features</h2><p>Add Live2D widget to web page. Compatible with PJAX.</p>
<p><img src="assets/screenshot-2.png" width="280"><img src="assets/screenshot-3.png" width="280"><img src="assets/screenshot-1.png" width="270"></p>
<p>(Note: The character models above are for demonstration purposes only and are not included in this repository.)</p>
<p>You can also check out example web pages:</p>
<ul>
<li>Check the effect in the lower left corner of <a href="https://zhangshuqiao.org/">Mimi’s Blog</a></li>
<li><a href="https://stevenjoezhang.github.io/live2d-widget/demo/demo.html">demo.html</a> to demonstrate basic functionality</li>
<li><a href="https://stevenjoezhang.github.io/live2d-widget/demo/login.html">login.html</a> to imitate the login interface of NPM</li>
</ul>
<h2 id="Usage"><a href="#Usage" class="headerlink" title="Usage"></a>Usage</h2><p>If you are a beginner or only need the basic functionality, you can simply add the following line of code to the <code>head</code> or <code>body</code> of your HTML page to load the widget:</p>
<pre><code class="xml">&lt;script src=&quot;https://fastly.jsdelivr.net/gh/stevenjoezhang/live2d-widget@latest/autoload.js&quot;&gt;&lt;/script&gt;
</code></pre>
<p>The placement of the code depends on how your website is built. For example, if you are using <a href="https://hexo.io/">Hexo</a>, you need to add the above code to the template file of your theme. The modification process is similar for pages generated using various template engines.<br>If your website uses PJAX, since the widget does not need to be refreshed on every page, make sure to place the script outside the PJAX refresh area.</p>
<p><strong>However, we strongly recommend configuring the widget yourself to make it more suitable for your website!</strong><br>If you are interested in customizing the widget, please refer to the detailed instructions below.</p>
<h2 id="Configuration"><a href="#Configuration" class="headerlink" title="Configuration"></a>Configuration</h2><p>You can refer to the source code of <code>autoload.js</code> to see the available configuration options. <code>autoload.js</code> will automatically load three files: <code>waifu.css</code>, <code>live2d.min.js</code>, and <code>waifu-tips.js</code>. <code>waifu-tips.js</code> creates the <code>initWidget</code> function, which is the main function for loading the widget. The <code>initWidget</code> function accepts an object-type parameter as the configuration for the widget. The following are the available options:</p>
<table>
<thead>
<tr>
<th>Option</th>
<th>Type</th>
<th>Default Value</th>
<th>Description</th>
</tr>
</thead>
<tbody><tr>
<td><code>waifuPath</code></td>
<td><code>string</code></td>
<td><code>https://fastly.jsdelivr.net/gh/stevenjoezhang/live2d-widget@latest/waifu-tips.json</code></td>
<td>Path to the widget resources, can be modified</td>
</tr>
<tr>
<td><code>apiPath</code></td>
<td><code>string</code></td>
<td><code>https://live2d.fghrsh.net/api/</code></td>
<td>API path, optional</td>
</tr>
<tr>
<td><code>cdnPath</code></td>
<td><code>string</code></td>
<td><code>https://fastly.jsdelivr.net/gh/fghrsh/live2d_api/</code></td>
<td>CDN path, optional</td>
</tr>
<tr>
<td><code>tools</code></td>
<td><code>string[]</code></td>
<td>see <code>autoload.js</code></td>
<td>Buttons of the loaded tools, optional</td>
</tr>
</tbody></table>
<p>Among them, the parameters <code>apiPath</code> and <code>cdnPath</code> only need to set one of them. <code>apiPath</code> is the URL of the backend API, which can be set up and modified by yourself (there are many things to modify, not discussed here). You can refer to <a href="https://github.com/fghrsh/live2d_api">live2d_api</a> for more information. On the other hand, <code>cdnPath</code> is used to load resources through CDN services like jsDelivr, which provides better stability.</p>
<h2 id="Customization"><a href="#Customization" class="headerlink" title="Customization"></a>Customization</h2><p>If the options provided in the “Configuration” section above are not enough to meet your needs, you can make modifications yourself. The directory structure of this repository is as follows:</p>
<ul>
<li><code>src/waifu-tips.js</code> contains the logic for the button and dialog box.</li>
<li><code>waifu-tips.js</code> is automatically generated by <code>src/waifu-tips.js</code> and it is not recommended to modify it directly.</li>
<li><code>waifu-tips.json</code> defines the triggering conditions (<code>selector</code>, CSS selector) and the displayed text when triggered (<code>text</code>).</li>
<li><code>waifu.css</code> is the style sheet for the virtual assistant.</li>
</ul>
<p>By default, the CSS selector rules in <code>waifu-tips.json</code> are effective for the Hexo <a href="http://github.com/next-theme/hexo-theme-next">NexT theme</a>, but you may need to modify or add new content to make it suitable for your own website.<br><strong>Warning: The content in <code>waifu-tips.json</code> may not be suitable for all age groups or appropriate to access during work. Please ensure their suitability when using them.</strong></p>
<p>To deploy the development testing environment of this project locally, you need to install Node.js and npm, then execute the following commands:</p>
<pre><code class="bash">git clone https://github.com/stevenjoezhang/live2d-widget.git
npm install
npm run build
</code></pre>
<p>If you have any questions, feel free to raise an issue. If you have any modification suggestions, welcome to submit a pull request.</p>
<h2 id="Deploy"><a href="#Deploy" class="headerlink" title="Deploy"></a>Deploy</h2><p>After making modifications locally, you can deploy the modified project on a server or load it via a CDN for use on a webpage.</p>
<h3 id="Using-CDN"><a href="#Using-CDN" class="headerlink" title="Using CDN"></a>Using CDN</h3><p>To customize the content, you can fork this repository and push the modified content to your own repository using <code>git push</code>. In this case, the usage method becomes:</p>
<pre><code class="xml">&lt;script src=&quot;https://fastly.jsdelivr.net/gh/username/live2d-widget@latest/autoload.js&quot;&gt;&lt;/script&gt;
</code></pre>
<p>Replace <code>username</code> with your GitHub username. To ensure the content of the CDN is refreshed correctly, you need to create a new git tag and push it to the GitHub repository. Otherwise, <code>@latest</code> in the URL will still point to the previous version. Additionally, CDN itself has caching, so the changes may take some time to take effect. Relevant documentation:</p>
<ul>
<li><a href="https://git-scm.com/book/en/v2/Git-Basics-Tagging">Git Basics - Tagging</a></li>
<li><a href="https://docs.github.com/en/repositories/releasing-projects-on-github/managing-releases-in-a-repository">Managing releases in a repository</a></li>
</ul>
<h3 id="Self-host"><a href="#Self-host" class="headerlink" title="Self-host"></a>Self-host</h3><p>Alternatively, you can directly host these files on your server instead of loading them via CDN.</p>
<ul>
<li>If you can connect to your host via <code>ssh</code>, clone the forked and modified code repository onto your server.</li>
<li>If your host cannot be accessed via <code>ssh</code> (e.g., a regular virtual host), modify the code locally and upload the files to the website directory on the host using <code>ftp</code> or similar methods.</li>
<li>If you are deploying a static blog using Hexo or similar tools, place the code of this project in the blog’s source file directory (e.g., the <code>source</code> directory). When redeploying the blog, the relevant files will be automatically uploaded to the corresponding paths. To prevent these files from being incorrectly modified by Hexo plugins, you may need to set <code>skip_render</code>.</li>
</ul>
<p>Afterwards, the entire project can be accessed through your domain name. You can try opening the <code>autoload.js</code> and <code>live2d.min.js</code> files in your browser and confirm that their content is complete and correct.<br>If everything is normal, you can proceed to modify the constant <code>live2d_path</code> in <code>autoload.js</code> to the URL of the <code>live2d-widget</code> directory. For example, if you can access <code>live2d.min.js</code> through the following URL:</p>
<pre><code>https://example.com/path/to/live2d-widget/live2d.min.js
</code></pre>
<p>then modify the value of <code>live2d_path</code> to:</p>
<pre><code>https://example.com/path/to/live2d-widget/
</code></pre>
<p>Make sure to include the trailing <code>/</code> in the path.<br>Once done, add the following code to the interface where you want to add the live2d-widget:</p>
<pre><code class="xml">&lt;script src=&quot;https://example.com/path/to/live2d-widget/autoload.js&quot;&gt;&lt;/script&gt;
</code></pre>
<p>This will load the widget.</p>
<h2 id="Thanks"><a href="#Thanks" class="headerlink" title="Thanks"></a>Thanks</h2><a href="https://www.browserstack.com/">
  <picture>
    <source media="(prefers-color-scheme: dark)" height="80" srcset="https://d98b8t1nnulk5.cloudfront.net/production/images/layout/logo-header.png?1469004780">
    <source media="(prefers-color-scheme: light)" height="80" srcset="https://live.browserstack.com/images/opensource/browserstack-logo.svg">
    <img alt="BrowserStack Logo" height="80" src="https://live.browserstack.com/images/opensource/browserstack-logo.svg">
  </picture>
</a>

<blockquote>
<p>Thanks to <a href="https://www.browserstack.com/">BrowserStack</a> for providing the infrastructure that allows us to test in real browsers!</p>
</blockquote>
<a href="https://www.jsdelivr.com">
  <picture>
    <source media="(prefers-color-scheme: dark)" height="80" srcset="https://raw.githubusercontent.com/jsdelivr/jsdelivr-media/master/white/svg/jsdelivr-logo-horizontal.svg">
    <source media="(prefers-color-scheme: light)" height="80" srcset="https://raw.githubusercontent.com/jsdelivr/jsdelivr-media/master/default/svg/jsdelivr-logo-horizontal.svg">
    <img alt="jsDelivr Logo" height="80" src="https://raw.githubusercontent.com/jsdelivr/jsdelivr-media/master/default/svg/jsdelivr-logo-horizontal.svg">
  </picture>
</a>

<blockquote>
<p>Thanks to jsDelivr for providing public CDN service.</p>
</blockquote>
<p>The code is modified based on this blog post:<br><a href="https://www.fghrsh.net/post/123.html">https://www.fghrsh.net/post/123.html</a></p>
<p>Thanks to <a href="https://hitokoto.cn/">Hitokoto</a> for providing the sentence API.</p>
<p>When you click on the paper airplane button of the virtual assistant, a hidden surprise will appear. This feature is from <a href="http://www.websiteasteroids.com/">WebsiteAsteroids</a>.</p>
<h2 id="More"><a href="#More" class="headerlink" title="More"></a>More</h2><p>For more information, you can refer to the following links:<br><a href="https://nocilol.me/archives/lab/add-dynamic-poster-girl-with-live2d-to-your-blog-02">https://nocilol.me/archives/lab/add-dynamic-poster-girl-with-live2d-to-your-blog-02</a><br><a href="https://github.com/xiazeyu/live2d-widget.js">https://github.com/xiazeyu/live2d-widget.js</a><br><a href="https://github.com/summerscar/live2dDemo">https://github.com/summerscar/live2dDemo</a></p>
<p>Regarding the backend API models:<br><a href="https://github.com/xiazeyu/live2d-widget-models">https://github.com/xiazeyu/live2d-widget-models</a><br><a href="https://github.com/xiaoski/live2d_models_collection">https://github.com/xiaoski/live2d_models_collection</a></p>
<p>In addition to that, there are desktop versions available:<br><a href="https://github.com/amorist/platelet">https://github.com/amorist/platelet</a><br><a href="https://github.com/akiroz/Live2D-Widget">https://github.com/akiroz/Live2D-Widget</a><br><a href="https://github.com/zenghongtu/PPet">https://github.com/zenghongtu/PPet</a><br><a href="https://github.com/LikeNeko/L2dPetForMac">https://github.com/LikeNeko/L2dPetForMac</a></p>
<p>And also Wallpaper Engine:<br><a href="https://github.com/guansss/nep-live2d">https://github.com/guansss/nep-live2d</a></p>
<h2 id="License"><a href="#License" class="headerlink" title="License"></a>License</h2><p>Released under the GNU General Public License v3<br><a href="http://www.gnu.org/licenses/gpl-3.0.html">http://www.gnu.org/licenses/gpl-3.0.html</a></p>
<p>This repository does not contain any models. The copyrights of all Live2D models, images, and motion data used for demonstration purposes belong to their respective original authors. They are provided for research and learning purposes only and should not be used for commercial purposes.</p>
<p>Official Live2D websites:<br><a href="https://www.live2d.com/en/">https://www.live2d.com/en/</a><br><a href="https://live2d.github.io/">https://live2d.github.io</a></p>
<p>Live2D Cubism Core is provided under the Live2D Proprietary Software License.<br><a href="https://www.live2d.com/eula/live2d-proprietary-software-license-agreement_en.html">https://www.live2d.com/eula/live2d-proprietary-software-license-agreement_en.html</a><br>Live2D Cubism Components are provided under the Live2D Open Software License.<br><a href="http://www.live2d.com/eula/live2d-open-software-license-agreement_en.html">http://www.live2d.com/eula/live2d-open-software-license-agreement_en.html</a></p>
<blockquote>
<p>The terms and conditions do prohibit modification, but obfuscating in <code>live2d.min.js</code> would not be considered illegal modification.</p>
</blockquote>
<p><a href="https://community.live2d.com/discussion/140/webgl-developer-licence-and-javascript-question">https://community.live2d.com/discussion/140/webgl-developer-licence-and-javascript-question</a></p>
<h2 id="Update-Log"><a href="#Update-Log" class="headerlink" title="Update Log"></a>Update Log</h2><p>On October 31, 2018, the original API provided by fghrsh was discontinued. Please update to the new address. Refer to the following article for more information:<br><a href="https://www.fghrsh.net/post/170.html">https://www.fghrsh.net/post/170.html</a></p>
<p>Starting from January 1, 2020, this project no longer depends on jQuery.</p>
<p>Starting from November 1, 2022, this project no longer requires users to separately load Font Awesome.</p>
